package com.skivingcloud.report.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.skivingcloud.common.exceptions.BusinessException;
import com.skivingcloud.common.utils.JacksonUtil;
import com.skivingcloud.report.dto.DataSourceDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import org.springframework.util.PropertyPlaceholderHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AnalysisUtil {

    private static final String placeholderPrefix = "${";
    private static final String placeholderSuffix = "}";
    private static final PropertyPlaceholderHelper helper =
            new PropertyPlaceholderHelper(placeholderPrefix, placeholderSuffix);

    public static String resolveParams(final Map<String, Object> param, String con) {
        con = helper.replacePlaceholders(con, (key -> param.get(key) + ""));
        return con;
    }

    private static final Pattern key = Pattern.compile("\\$\\{(.*?)\\}");

    public static List<String> findParamKeys(String con) {
        Matcher m = key.matcher(con);
        List ret = new ArrayList();
        while (m.find()) {
            ret.add(m.group(1));
        }
        return ret;
    }

    /**
     * http接口配置转换
     *
     * @param dto 参数json
     * @return 转换后参数
     */
    public static DataSourceDto analysisHttpConfig(DataSourceDto dto) throws JsonProcessingException {
        Map<String, String> json = JacksonUtil.jsonStringToMap(dto.getSourceConfig());
//        JSONObject json = JSONUtil.parseObj(sourceConfig);
        Assert.isTrue(json.containsKey("apiUrl"), "apiUrl不能为空");
        Assert.isTrue(json.containsKey("method"), "method不能为空");
        Assert.isTrue(json.containsKey("header"), "header不能为空");
        Assert.isTrue(json.containsKey("body"), "body不能为空");
        String apiUrl = json.get("apiUrl");
        String method = json.get("method");
        String header = json.get("header");
        String body = json.get("body");
        //解决url中存在的动态参数
        apiUrl = transform(dto.getContextData(), apiUrl);
        //请求头中动态参数
        header = transform(dto.getContextData(), header);
        dto.setApiUrl(apiUrl);
        dto.setMethod(method);
        dto.setHeader(header);
        dto.setBody(body);
        return dto;
    }

    /**
     * 参数替换
     *
     * @param contextData 参数
     * @param dynSentence 参数名
     * @return 参数替换
     */
    public static String transform(Map<String, Object> contextData, String dynSentence) {
        if (StringUtils.isBlank(dynSentence)) {
            return dynSentence;
        }
        if (dynSentence.contains("${")) {
            dynSentence = resolveParams(contextData, dynSentence);
        }
        if (dynSentence.contains("${")) {
            throw new BusinessException(dynSentence + "不满足参数格式");
        }
        return dynSentence;
    }

    /**
     * 获取关系型数据库配置
     * @param dto 配置
     * @return 配置
     */
    public static DataSourceDto analysisRelationalDbConfig(DataSourceDto dto) throws JsonProcessingException {
        Map<String, String> json = JacksonUtil.jsonStringToMap(dto.getSourceConfig());
        Assert.isTrue(json.containsKey("jdbcUrl"), "jdbcUrl不能为空");
        Assert.isTrue(json.containsKey("driverName"), "driverName不能为空");
        String jdbcUrl = json.get("jdbcUrl");
        String username = json.get("username");
        String password = json.get("password");
        String driverName = json.get("driverName");
        dto.setJdbcUrl(jdbcUrl);
        dto.setDriverName(driverName);
        dto.setUsername(username);
        dto.setPassword(password);
        return dto;
    }
}
